package com.kehao.leetcode.middle.array;

import org.junit.Test;

import java.util.*;

public class GroupingOfHeterotopicWordsSolution {
    public List<List<String>> groupAnagrams(String[] strs) {
        HashMap<String,List<String>> map = new HashMap<>();
        for(String str:strs){
            char[] chars = str.toCharArray();
            Arrays.sort(chars);
            String sKey = new String(chars);
            if(!map.containsKey(sKey)){
                ArrayList<String> stringList = new ArrayList<>();
                stringList.add(str);
                map.put(sKey,stringList);
            }else {
                map.get(sKey).add(str);
            }
        }
        return new ArrayList<>(map.values());
    }

    @Test
    public void test01(){
        String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
        System.out.println(groupAnagrams(strs));
    }
}
